package com.mini.controller;

import com.mini.config.CheckLogin;
import com.mini.req.RequirementPlanSaveReq;
import com.mini.res.RequirementPlanDistributionRes;
import com.mini.res.RequirementPlanFindRes;
import com.mini.service.BusinessRequirementPlanService;
import com.mini.vo.BusinessResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 项目名称：traditional-business
 * 类 名 称：BusinessRequirementPlanController
 * 类 描 述：需求规划
 * 创建时间：2020/12/15 下午7:53
 * 创 建 人：zhanghuiying
 */
@RestController
@Api(tags = {"需求规划"})
@RequestMapping("/RequirementPlan/")
@Slf4j
public class BusinessRequirementPlanController {

    @Autowired
    private BusinessRequirementPlanService businessRequirementPlanService;

    @CheckLogin
    @GetMapping("copyRequirementPlan")
    @ApiOperation(value = "复制昨日需求规划")
    public BusinessResult<Boolean> copyRequirementPlan(@ApiParam(value = "token",required = true) @RequestHeader(value = "Authorization") String Authorization,
                                                       @ApiParam(value = "量子军团id",required = true) @RequestParam(value = "quantumArmyGroupId") Long quantumArmyGroupId,
                                                       @ApiParam(value = "要复制的日期",required = true) @RequestParam(value = "planDate") String planDate){
        try {
            DateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
            Date date = formatter.parse(planDate);
            Boolean res= businessRequirementPlanService.copyRequirementPlan(Authorization,quantumArmyGroupId,date);
            return BusinessResult.createSuccessInstance(res);
        }catch (Exception e){
            log.info(e.getMessage());
            return BusinessResult.createInstance(1111L,e.getMessage(),false);
        }
    }

//    @CheckLogin
//    @DeleteMapping("deleteRequirementPlan")
//    @ApiOperation(value = "删除需求规划列表")
//    public BusinessResult<Boolean> deleteRequirementPlan(@ApiParam(value = "表Id",required = true) @RequestParam(value = "tableId") Long tableId,
//                                                         @ApiParam(value = "token",required = true) @RequestHeader(value = "Authorization")String Authorization){
//        try{
//            Boolean res=businessRequirementPlanService.deleteRequirementPlan(Authorization,tableId);
//            return BusinessResult.createSuccessInstance(res);
//        }catch (Exception e){
//            log.info(e.getMessage());
//            return BusinessResult.createInstance(1111L,e.getMessage(),false);
//        }
//    }

    @CheckLogin
    @PostMapping("saveRequirementPlan")
    @ApiOperation(value = "保存需求规划")
    public BusinessResult<Boolean> saveRequirementPlan(@ApiParam(value = "token",required = true) @RequestHeader(value = "Authorization") String Authorization,
                                                       @ApiParam(value = "保存数据体",required = true) @RequestBody RequirementPlanSaveReq requirementPlanSaveReq){
        try {
            Boolean res= businessRequirementPlanService.saveRequirementPlan(Authorization, requirementPlanSaveReq);
            return BusinessResult.createSuccessInstance(res);
        }catch (Exception e){
            log.info(e.getMessage(),e);
            return BusinessResult.createInstance(1111L,e.getMessage(),false);
        }
    }

    @CheckLogin
    @GetMapping("queryRequirementPlanContent")
    @ApiOperation(value = "查询需求规划内容信息")
    public BusinessResult<RequirementPlanFindRes> queryRequirementPlanContent(@ApiParam(value = "token" ,required = true) @RequestHeader String Authorization,
                                                                            @ApiParam(value = "量子军团id",required = true) @RequestParam Long quantumArmyGroupId,
                                                                            @ApiParam(value = "规划日期",required = true) @RequestParam String planDate){
        try{
            RequirementPlanFindRes requirementPlanFindRes = businessRequirementPlanService.queryRequirementPlanCount(Authorization,quantumArmyGroupId,planDate);
            if (Objects.isNull(requirementPlanFindRes)){
                requirementPlanFindRes=new RequirementPlanFindRes();
            }
            return BusinessResult.createSuccessInstance(requirementPlanFindRes);
        }catch (Exception e){
            log.info(e.getMessage(),e);
            return BusinessResult.createInstance(1111L,e.getMessage(),"");
        }
    }

    @CheckLogin
    @GetMapping("queryRequirementPlanDistribution")
    @ApiOperation(value = "查询机会分配的需求规划信息 测试")
    public BusinessResult<RequirementPlanDistributionRes> queryRequirementPlanDistribution(@ApiParam(value = "token" ,required = true) @RequestHeader String Authorization,
                                                                                           @ApiParam(value = "用户id",required = true) @RequestParam Long userId,
                                                                                           @ApiParam(value = "项目id",required = true) @RequestParam Integer projectId,
                                                                                           @ApiParam(value = "渠道code",required = true) @RequestParam String channelCode,
                                                                                           @ApiParam(value = "省份code",required = true) @RequestParam String province
                                                                                           ){
        List<RequirementPlanDistributionRes> res=businessRequirementPlanService.queryRequirementPlanDistribution(userId,projectId,channelCode,province);
        return BusinessResult.createSuccessInstance(res);
    }

    @CheckLogin
    @GetMapping("updateRequirementPlanDistribution")
    @ApiOperation(value = "更新机会分配的需求规划信息 测试")
    public BusinessResult<Boolean> updateRequirementPlanDistribution(@ApiParam(value = "token" ,required = true) @RequestHeader String Authorization,
                                                                     @ApiParam(value = "量子军团id",required = true) @RequestParam Integer quantumArmyGroupId,
                                                                     @ApiParam(value = "项目id",required = true) @RequestParam Integer projectId,
                                                                     @ApiParam(value = "渠道code",required = true) @RequestParam String channelCode,
                                                                     @ApiParam(value = "省份code",required = true) @RequestParam String province,
                                                                     @ApiParam(value = "添加需求量",required = true)@RequestParam Integer allotCount
                                                                     ){
       Boolean res= businessRequirementPlanService.updateRequirementPlanDistribution(quantumArmyGroupId,projectId,channelCode,province,allotCount);
       return BusinessResult.createSuccessInstance(res);
    }
}
